<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
    <link rel="stylesheet" type="text/css" href="/build/css/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/css/my-style.css">
</head>
<style>
    .layui-layer-setwin .layui-layer-max {
        display: none;
    }
    .layui-input-block {
        margin-left: 150px;
    }

    .layui-form-label {
        width: 110px;
    }
</style>
<body>
<!-- 弹框content -->
<div id="modal-content1" style="padding:20px;">
    <form class="layui-form" action="">
        <input type="hidden" id="userId" th:value="${user.getId()}">

        <div class="layui-form-item">
            <label class="layui-form-label">登录名<span
                    style="color: red">★</span>：</label>
            <div class="layui-input-block">
                <input type="text" name="loginName" required lay-verify="required"
                       placeholder="请输入登录名"
                       autocomplete="off" class="layui-input"
                       th:value="${user.getLoginName()}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">真实名称<span
                    style="color: red">★</span>：</label>
            <div class="layui-input-block">
                <input type="text" name="userName" required lay-verify="required"
                       placeholder="请输入真实名称"
                       autocomplete="off" class="layui-input" th:value="${user.getUserName()}">
            </div>
        </div>
        <input name="deptId" type="hidden" id="treeId"th:value="${user.getDept().getId()}"/>
        <div class="layui-form-item">
            <label class="layui-form-label">部门名称<span
                    style="color: red">★</span>：</label>
            <div class="layui-input-block">
                <input class="layui-input" type="text" name="deptName" onclick="selectDeptTree()"
                       lay-verify="required"  th:value="${user.getDept().getDeptName()}"
                       readonly="true" id="treeName">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="text" name="email"
                       placeholder="请输入邮箱账户"
                       autocomplete="off" class="layui-input" th:value="${user.getEmail()}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="text" name="phonenumber"
                       placeholder="请输入手机号"
                       autocomplete="off" class="layui-input" th:value="${user.getPhonenumber()}">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <select name="sex" lay-verify="required" id="sex">
                    <option th:value="${user.getSex() eq '0'? '1':'0'} "
                            th:text="${user.getSex() eq '0'?'女':'男'}"></option>
                    <option th:value="${user.getSex()} "
                            th:text="${user.getSex() eq '0'?'男':'女'}"></option>

                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">岗位<span
                    style="color: red">★</span>：</label>
            <div class="layui-input-block">
                <select id="post" name="post" lay-verify="required" multiple="">
                    <option th:each="post:${postList}" th:value="${post.Id}"
                            th:text="${post.postName}" th:selected="${post.flag}"
                            th:disabled="${post.status == '1'}"></option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">角色<span
                    style="color: red">★</span>：</label>
            <div class="layui-input-block">
                <select xm-select="role">
                    <option value=""></option>
                    <option th:each="role :${roleList}" th:value="${role.getId()}"
                            th:text="${role.roleName}"></option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">启用/禁用</label>
            <div class="layui-input-block">
                <input type="checkbox" name="status" id="status" lay-skin="switch"  lay-text="启用|禁用"
                       th:value="${user.getStatus()}" th:checked="${user.getStatus() eq '0'}"
                       lay-filter="switchStatus">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入内容" id="remark"
                          class="layui-textarea" th:text="${user.getRemark()}"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="formDemo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/js/jquery.min.js"></script>
<script src="/plugins/layui/layui.js"></script>
<script src="/js/ry-ui.js"></script>
<script th:inline="javascript">
    // 获取角色list
    var roleList = [[${roleList}]];
    var roleSelect = [];
    for (var i = 0; i < roleList.length; i++) {
        if (roleList[i].flag === true) {
            roleSelect.push(roleList[i].id);
        }
    }
</script>
<script>
    layui.config({
        base: '/plugins/layui/'    //此处写的相对路径, 实际以项目中的路径为准
    }).extend({
        formSelects: 'formSelects'
    });
    // 增加项目表单
    layui.use(['form', 'layedit', 'laydate', 'jquery', 'formSelects'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit,
            laydate = layui.laydate,
            formSelects = layui.formSelects;

        // 设置角色下拉框的默认值
        formSelects.value('role', roleSelect);

        //常规用法
        laydate.render({
            elem: '#startDate'
        });
        laydate.render({
            elem: '#endDate'
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            }
            , pass: [/(.+){6,12}$/, '密码必须6到12位']
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });

        //监听指定开关
        form.on('switch(switchStatus)', function (data) {
            this.value = this.checked ? "0" : "1";
        });


        var $ = layui.$;
        //监听提交
        form.on('submit(formDemo)', function (data) {
            var roleIds = formSelects.value('role', 'valStr');
            var postIds = $.form.selectSelects("post");
            data.field.roleIds = roleIds;
            data.field.postIds = postIds;
            data.field.status = $("#status").val();
            if (roleIds == '' || roleIds == null || roleIds == undefined) {
                layer.msg('请选择角色！');
                return false;
            }
            if (postIds == '' || postIds == null || postIds == undefined) {
                layer.msg('请选择岗位！');
                return false;
            }
            $.ajax({
                type: 'PUT',
                url: "/system/user/update/" + $("#userId").val(),
                data: data.field,
                success: function (res) {
                    if (res.code == 0) {
                        layer.msg("修改成功",{
                            time: 800
                        },function () {
                            // 关闭自己
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);//页面刷新
                            window.parent.location = "/system/user/view";//页面刷新
                        });
                    } else {
                        layer.msg("修改失败" + res.msg);
                    }
                }
            });

            return false;
        });

    });

    /*用户管理-新增-选择部门树*/
    function selectDeptTree() {
        var treeId = $("#treeId").val();
        var deptId = treeId == null || treeId == "" ? "100" : treeId;
        var url = "/system/dept/selectDeptTree/" + deptId;
        // layer.open("选择部门", url, '380', '380');
        layer.open({
            title: "选择部门",
            type: 2,
            content: url,
            area: ['50%', '60%'],
            shadeClose: true
        });
    }
</script>
</body>
</html>